package com;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-15
 * Time: 21:31
 */

public class TaoBao {
    public void Tao() throws InterruptedException {

        ChromeOptions option = new ChromeOptions();
        //关闭受自动软件的控制
        option.addArguments("disable-infobars");
        List<Object> list=new ArrayList<>();
        list.add("enable-automation");
        option.setExperimentalOption("excludeSwitches",list);


        //在文件中找到指定的打开谷歌浏览器的驱动
        System.setProperty("webdriver.chrome.driver", "E:/Java/bin/chromedriver.exe");



        //创建一个驱动对象 来打开浏览器
        ChromeDriver driver = new ChromeDriver();
        //输入访问的网址
        driver.get("https://www.taobao.com/?spm=a2e0b.20350158.1581860521.1.3127468aenn9Wu&pid=mm_26632360_8858797_29866178&union_lens=recoveryid%3A201_33.7.72.85_15130448_1668518879505%3Bprepvid%3A201_33.7.239.97_15131466_1668518886744&clk1=afb1dd0df153b03d60e2c123a0246911?spm=a2e0b.20350158.1581860521.1.3127468aenn9Wu&pid=mm_26632360_8858797_29866178&union_lens=recoveryid%3A201_33.7.72.85_15130448_1668518879505%3Bprepvid%3A201_33.7.239.97_15131466_1668518886744&clk1=afb1dd0df153b03d60e2c123a0246911");

        //隐式等待 等待时间1秒
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));

        //点击 《亲,请登录》
        driver.findElement(By.cssSelector("#J_LoginInfoHd > a.h")).click();

        //找到输入输入框 输入账号密码
        driver.findElement(By.cssSelector("#fm-login-id")).sendKeys("17749981834");
        driver.findElement(By.cssSelector("#fm-login-password")).sendKeys("lqh520gp99");

        String pageSource1= driver.getPageSource();
        if(pageSource1.contains("next-btn next-btn-primary next-btn-large")){
            WebElement element = driver.findElement(By.cssSelector("#\\`nc_1_refresh1\\`"));
            System.out.println("LOGIN----------"+element.getText()+"-----------------LOGIN");
            element.click();
            Thread.sleep(3000L);
        }

        //点击提交按钮
        driver.findElement(By.cssSelector("#login-form > div.fm-btn > button")).click();

        //成功登录之后 点击我的购物车
        driver.findElement(By.cssSelector("#J_MtSideMenu > div > dl > dd:nth-child(2) > a")).click();

        Thread.sleep(3000);
        driver.quit();
    }

}
